package com.wolfsea.defineappframedemo
import android.os.Bundle
import androidx.appcompat.widget.AppCompatButton
import com.wolfsea.defineappframedemo.databinding.ActivityMainBinding

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

    private var lastBelongIndex = -1
    private var clickIndex = 0

    private var btnArray = arrayOfNulls<AppCompatButton>(8)

    override fun init(savedInstanceState: Bundle?) {

        btnArray = arrayOf(
            mBinding.btn1,
            mBinding.btn2,
            mBinding.btn3,
            mBinding.btn4,
            mBinding.btn5,
            mBinding.btn6,
            mBinding.btn7,
            mBinding.btn8
        )

        for (i in btnArray.indices) {
            btnArray[i]?.setOnClickListener {
                if (clickIndex != i) {
                    mBinding.contentItemLayout.skip2Item(i)
                    clickIndex = i
                    lastBelongIndex = clickIndex
                    for (j in btnArray.indices) {
                        setBackground(j, clickIndex, j)
                    }
                }
            }
            setBackground(i, clickIndex, i)
        }

        mBinding.contentItemLayout.updateCallback(object :
            ContentItemLayout.ICalcBelongedIndexCallback {
            override fun calcBelongedIndex(belongedIndex: Int) {
                if (lastBelongIndex != belongedIndex) {
                    for (i in btnArray.indices) {
                        setBackground(i, belongedIndex, i)
                    }
                    clickIndex = belongedIndex
                    lastBelongIndex = belongedIndex
                }
            }
        })

    }

    private fun setBackground(btnIndex: Int, clickIndex: Int, arrayIndex: Int) {
        btnArray[btnIndex]?.setBackgroundColor(
            if (clickIndex == arrayIndex) resources.getColor(R.color.color_00ffff)
            else resources.getColor(R.color.color_eae5ef))
    }

}